package com.yicheng.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yicheng.controller.utils.R;
import com.yicheng.entity.Goods;
import com.yicheng.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @GetMapping("/getGoods")
    public R getGoods(Integer pageSize, Integer currentPage){
//        System.out.println(currentPage);
//        System.out.println(pageSize);
        R r = new R();
        try {
            IPage<Goods> page = goodsService.getPage(currentPage, pageSize);
            if(currentPage > page.getPages()){
                page = goodsService.getPage((int) page.getPages(), pageSize);
            }
            r.setList(page);
            r.setMsg("获取商品数据成功");
        }catch (Exception e){
            r.setStatus(false);
            r.setMsg(e.getMessage());
        }
        return r;
    }

    @GetMapping("/getOneGoods")
    public R getOneGoods(Integer id){
        R r = new R();
        try {
            r.setList(goodsService.getById(id));
            r.setMsg("获取该商品信息成功");
        }catch (Exception e){
            r.setStatus(false);
            r.setMsg(e.getMessage());
        }
        return r;
    }
}
